import { Dispatch } from 'redux'
import {
    getRankingPartitionsRequest,
    getRankingRequest,
    getPartitionsRequest
} from '@/api/request'
import * as actionTypes from "../action-types";

// 页面级别action 
// promise.all
export const getHomeDataAction = (rId:string) => {
    return (dispatch:Dispatch) => {
        dispatch(setLoading(true))
        return Promise.all([
            getPartitionsRequest(),
            getRankingPartitionsRequest(),
            getRankingRequest(rId),
        ]).then(([PartitionsResult,rangkingPartitionsResult,videoResult]) => {
            let partitions = PartitionsResult.data[0];
            partitions = partitions.filter((partition:any) => 
                     [13, 23, 11, 177].indexOf(partition.id) === -1);
            dispatch(setoneLevelPartitions(partitions))
            dispatch(setRankingPartitions(rangkingPartitionsResult.data))
            dispatch(setRankingVideos(videoResult.data.list))
            dispatch(setLoading(false))
        })
    }
}

export const setLoading = (data:boolean) => ({
    type:actionTypes.SET_LOADING,
    data
})

export const setoneLevelPartitions = (data:any[]) => ({
    type:actionTypes.SET_ONE_LEVEL_PARTITIONS,
    data
})

export const setRankingPartitions = (data:any[]) => ({
    type:actionTypes.SET_RANKING_PARTITIONS,
    data
})

export const setRankingVideos = (data:any[]) => ({
    type:actionTypes.SET_RANKING_VIDEOS,
    data
})